var table;
var index;
var _table;
(function($){

    $.extend({
        initTableTree:function(options){
            _table = $("#table").bootstrapTable({
                url: options.url,
                method:'post',
                // 标示主键列
                idField: options.idField,
                showColumns: true,
                responseHandler:function(res,jqXHR){
                    return res.data;
                },
                queryParams:function (params) {


                    let f= $("#queryForm").serialize();

                },
                columns: options.columns,
                // 规定在哪一列上出现树状节点的图标
                treeShowField: options.treeShowField,
                contentType:'application/x-www-form-urlencoded',
                toolbar:$("#toolbar"),
                // 标示层级关系的列  parentId
                parentIdField: 'parentId',
                onCheck:function(){
                    $.check1();
                },
                onCheckAll:function(){
                    $.check1();
                },
                onUncheck:function(){
                    $.uncheck1();
                },
                onUncheckAll:function(){
                    $.uncheck1();
                },
                onPostBody: function() {
                    // 根据定义的treeShowField和parentIdField
                    var columns = $table.bootstrapTable('getOptions').columns

                    if (columns && columns[0][1].visible) {
                        $table.treegrid({
                            treeColumn: 1,
                            onChange: function() {
                                $table.bootstrapTable('resetView')
                            }
                        })
                    }
                }
            })
        },
        initTable:function(options){
            table =$('#table').bootstrapTable({
                height:options.height,

                url:options.url,
                method:'post',
                locale: 'zh_CN',
                pagination:true,
                sidePagination:'server',
                pageNumber:1,
                pageSize:15,
                showColumns:true,
                showFullscreen:true,
                showRefresh:true,
                contentType:'application/x-www-form-urlencoded',
                columns: options.columns,
                toolbar:$("#toolbar"),
                onCheck:function(){
                    $.check1();
                },
                onCheckAll:function(){
                    $.check1();
                },
                onUncheck:function(){
                    $.uncheck1();
                },
                onUncheckAll:function(){
                    $.uncheck1();
                },
                responseHandler:function(res){
                    let data={total:res.data.total,rows:res.data.list}
                    return data;
                },
                queryParams:function (params) {
                    let f= $("#queryForm").serialize();
                    return f+'&pageNumber='+this.pageNumber;
                }
            })
        },
        refresh:function(){
            $("#table").bootstrapTable('refresh');
        },
        check1:function(){
            let data =  $("#table").bootstrapTable('getSelections');
            if(data.length>0){
                $("#edit").removeAttr("disabled");
                $("#deletes").removeAttr("disabled");
            }
        },
        uncheck1:function(){
            let data =  $("#table").bootstrapTable('getSelections');
            if(data.length==0){
                $("#edit").attr("disabled",true);
                $("#deletes").attr("disabled",true);
            }
        },
        modal:{
            delete_:function(url){
                $.ajax({
                    url:url,
                    type:'DELETE',
                    dataType:'json',
                    success:function (ret) {
                        if(ret.code==200){
                            layer.msg(ret.msg,function(){
                                refresh();
                            });
                        }
                        if(ret.code!=200){
                            layer.msg(ret.msg);
                        }
                    }
                });
            },
            post:function(url,data,callback){
                $.ajax({
                    url:url,
                    type:'post',
                    dataType:'json',
                    data:data,
                    success:function (ret) {
                        if(callback!=undefined){
                            callback.call(this,ret);
                        }
                    }
                })
            },
            get:function (url,data,callback) {
                $.ajax({
                    url:url,
                    type:'get',
                    dataType:'json',
                    data:data,
                    success:function (ret) {
                        if(callback!=undefined){
                            this.call(callback);
                        }
                    }
                })
            }
        },
        operator:{
            openWin:function(title,url){
                index = layer.open({
                    // 使用iframe弹出
                    type: 2,
                    //　弹出层标题
                    title: title,
                    // 弹出层的大小
                    area: ['90%','480px'],
                    // iframe的url
                    content: url //iframe的url
                });
            }
        }
    })
})(jQuery)